package com.luca.web.controller.monitor;

import com.luca.common.annotation.Log;
import com.luca.common.core.controller.BaseController;
import com.luca.common.core.domain.JsonResult;
import com.luca.common.core.page.TableDataInfo;
import com.luca.common.enums.BusinessType;
import com.luca.system.service.ISysUserOnlineService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;

/**
 * 在线用户监控
 *
 * @author luca
 */
@RestController
@RequestMapping("/monitor/online")
public class SysUserOnlineController extends BaseController {

    private final ISysUserOnlineService userOnlineService;

    public SysUserOnlineController(ISysUserOnlineService userOnlineService) {
        this.userOnlineService = userOnlineService;
    }

    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('monitor:online:list')")
    public TableDataInfo list(String ipaddr, String userName) {
        var list = userOnlineService.selectOnlineUsers(userName, ipaddr);
        Collections.reverse(list);
        list.removeAll(Collections.singleton(null));
        return getDataTable(list);
    }

    /**
     * 强退用户
     */
    @PreAuthorize("@ss.hasPermi('monitor:online:forceLogout')")
    @Log(title = "在线用户", businessType = BusinessType.FORCE)
    @DeleteMapping("/{tokenId}")
    public JsonResult forceLogout(@PathVariable String tokenId) {
        userOnlineService.forceLogout(tokenId);
        return JsonResult.success();
    }

}
